<template>
  <view class="container">
      <view class="goods-list">
        <view v-for="(item,i) in goodList" :key="i" @click="gotoDetail(item)">
         <my-goods :item="item"></my-goods>
        </view>
      </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求参数
        queryObj: {
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodList :[],
        total: 0,
        // 是否正在请求数据
        isloading: false
       

      };
    },
    onLoad(options){
      this.queryObj.query = options.query||''
      this.queryObj.cid = options.cid ||''
      this.getGoodList()
    },
    methods:{
      async getGoodList(cb) {
         this.isloading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        } else {
          this.goodList = [...this.goodList,...res.message.goods]
          this.total = res.message.total
        }
         // ** 关闭节流阀
          this.isloading = false
          cb && cb()
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      }
    },
    onReachBottom(){
      // 判断是否还有下一页数据
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
      if (this.isloading) return
        this.queryObj.pagenum++
        this.getGoodList() 
    },
    onPullDownRefresh() {
      this.queryObj.pagenum=1
      this.total = 0
      this.isloading = false
      this.goodList = []
      this.getGoodList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">
.container {
  background: #FFFFFF;
}

</style>
